<template>
  <div class="cms-comments-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>评论管理</span>
          <el-space>
            <el-badge :value="pendingCount" class="item">
              <el-button type="primary" @click="handlePendingFilter">
                <el-icon><Bell /></el-icon>
                待审核评论
              </el-button>
            </el-badge>
          </el-space>
        </div>
      </template>

      <!-- 搜索区域 -->
      <div class="search-box">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="评论内容">
            <el-input 
              v-model="searchForm.content" 
              placeholder="请输入评论内容"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="文章标题">
            <el-input 
              v-model="searchForm.articleTitle" 
              placeholder="请输入文章标题"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select 
              v-model="searchForm.status" 
              placeholder="请选择状态"
              clearable
              style="width: 120px"
            >
              <el-option label="全部" value="" />
              <el-option label="待审核" value="pending" />
              <el-option label="已通过" value="approved" />
              <el-option label="已拒绝" value="rejected" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              查询
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 批量操作 -->
      <div class="toolbar">
        <el-button 
          type="success" 
          :disabled="selectedIds.length === 0"
          @click="handleBatchApprove"
        >
          <el-icon><Check /></el-icon>
          批量通过
        </el-button>
        <el-button 
          type="danger" 
          :disabled="selectedIds.length === 0"
          @click="handleBatchDelete"
        >
          <el-icon><Delete /></el-icon>
          批量删除
        </el-button>
      </div>

      <!-- 表格区域 -->
      <el-table 
        :data="tableData" 
        border 
        stripe
        v-loading="loading"
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="content" label="评论内容" min-width="250" show-overflow-tooltip />
        <el-table-column prop="articleTitle" label="所属文章" width="200" show-overflow-tooltip />
        <el-table-column prop="userName" label="评论用户" width="120" />
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="ip" label="IP地址" width="130" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="评论时间" width="180" sortable />
        <el-table-column label="操作" width="280" align="center" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="row.status === 'pending'"
              type="success" 
              size="small" 
              link
              @click="handleApprove(row)"
            >
              <el-icon><Check /></el-icon>
              通过
            </el-button>
            <el-button 
              v-if="row.status === 'pending'"
              type="warning" 
              size="small" 
              link
              @click="handleReject(row)"
            >
              <el-icon><Close /></el-icon>
              拒绝
            </el-button>
            <el-button 
              type="primary" 
              size="small" 
              link
              @click="handleReply(row)"
            >
              <el-icon><ChatDotRound /></el-icon>
              回复
            </el-button>
            <el-button 
              type="danger" 
              size="small" 
              link
              @click="handleDelete(row)"
            >
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 回复评论弹窗 -->
    <el-dialog
      v-model="replyDialogVisible"
      title="回复评论"
      width="600px"
    >
      <el-form label-width="100px">
        <el-form-item label="原评论">
          <el-text>{{ selectedComment?.content }}</el-text>
        </el-form-item>
        <el-form-item label="回复内容">
          <el-input 
            v-model="replyContent" 
            type="textarea"
            :rows="4"
            placeholder="请输入回复内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="replyDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleReplySubmit">
            发送回复
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Bell, Search, Refresh, Check, Close, Delete, ChatDotRound 
} from '@element-plus/icons-vue'

// 待审核数量
const pendingCount = ref(0)

// 搜索表单
const searchForm = reactive({
  content: '',
  articleTitle: '',
  status: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)
const selectedIds = ref([])

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

// 回复对话框
const replyDialogVisible = ref(false)
const selectedComment = ref(null)
const replyContent = ref('')

// 状态类型
const getStatusType = (status) => {
  const typeMap = {
    pending: 'warning',
    approved: 'success',
    rejected: 'danger'
  }
  return typeMap[status] || 'info'
}

// 状态文本
const getStatusText = (status) => {
  const textMap = {
    pending: '待审核',
    approved: '已通过',
    rejected: '已拒绝'
  }
  return textMap[status] || status
}

// 待审核筛选
const handlePendingFilter = () => {
  searchForm.status = 'pending'
  handleSearch()
}

// 查询
const handleSearch = () => {
  pagination.page = 1
  loadData()
}

// 重置
const handleReset = () => {
  searchForm.content = ''
  searchForm.articleTitle = ''
  searchForm.status = ''
  handleSearch()
}

// 审核通过
const handleApprove = (row) => {
  ElMessageBox.confirm('确定要通过该评论吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'success'
  }).then(() => {
    ElMessage.success('评论已通过')
    loadData()
  }).catch(() => {})
}

// 审核拒绝
const handleReject = (row) => {
  ElMessageBox.confirm('确定要拒绝该评论吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('评论已拒绝')
    loadData()
  }).catch(() => {})
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该评论吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    loadData()
  }).catch(() => {})
}

// 批量通过
const handleBatchApprove = () => {
  ElMessageBox.confirm(
    `确定要通过选中的 ${selectedIds.value.length} 条评论吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'success'
    }
  ).then(() => {
    ElMessage.success('批量审核通过成功')
    loadData()
  }).catch(() => {})
}

// 批量删除
const handleBatchDelete = () => {
  ElMessageBox.confirm(
    `确定要删除选中的 ${selectedIds.value.length} 条评论吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('批量删除成功')
    loadData()
  }).catch(() => {})
}

// 回复
const handleReply = (row) => {
  selectedComment.value = row
  replyContent.value = ''
  replyDialogVisible.value = true
}

// 提交回复
const handleReplySubmit = () => {
  if (!replyContent.value) {
    ElMessage.warning('请输入回复内容')
    return
  }
  ElMessage.success('回复成功')
  replyDialogVisible.value = false
}

// 选择改变
const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

// 分页改变
const handleSizeChange = (size) => {
  pagination.size = size
  loadData()
}

const handleCurrentChange = (page) => {
  pagination.page = page
  loadData()
}

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    tableData.value = [
      {
        id: 1,
        content: '这篇文章写得非常好，对我很有帮助！',
        articleTitle: '如何高效使用ERP系统提升企业管理效率',
        userName: '张三',
        email: 'zhangsan@example.com',
        ip: '192.168.1.100',
        status: 'pending',
        createTime: '2024-10-16 10:30:00'
      },
      {
        id: 2,
        content: '感谢分享，期待更多类似的内容',
        articleTitle: '2024年制造业数字化转型趋势分析',
        userName: '李四',
        email: 'lisi@example.com',
        ip: '192.168.1.101',
        status: 'approved',
        createTime: '2024-10-15 14:20:00'
      },
      {
        id: 3,
        content: '有些观点我不太认同，建议再深入研究一下',
        articleTitle: '新版本功能介绍：智能预测系统上线',
        userName: '王五',
        email: 'wangwu@example.com',
        ip: '192.168.1.102',
        status: 'approved',
        createTime: '2024-10-14 09:00:00'
      },
      {
        id: 4,
        content: '垃圾内容',
        articleTitle: '从零开始搭建企业CMS系统',
        userName: '恶意用户',
        email: 'spam@example.com',
        ip: '192.168.1.103',
        status: 'rejected',
        createTime: '2024-10-13 16:45:00'
      },
      {
        id: 5,
        content: '能否提供更详细的实施案例？',
        articleTitle: '企业数据安全最佳实践指南',
        userName: '赵六',
        email: 'zhaoliu@example.com',
        ip: '192.168.1.104',
        status: 'pending',
        createTime: '2024-10-16 08:15:00'
      }
    ]
    
    pendingCount.value = tableData.value.filter(item => item.status === 'pending').length
    pagination.total = tableData.value.length
  } catch (error) {
    ElMessage.error('加载数据失败：' + error.message)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.cms-comments-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-box {
  margin-bottom: 20px;
}

.toolbar {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>

